//
//  NewAniViewController.m
//  Tiger
//
//  Created by  on 12-7-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NewAniView.h"
#import <QuartzCore/QuartzCore.h>

#define  ViewScaleValue 0.5f
#define  ShowViewWidth 220
#define  ShowViewHeight 300

#define ShowImgTag  0x100

typedef enum {
    AniamteOverStatus_None = 0x10,
    AniamteOverStatus_ToLeft,
    AniamteOverStatus_ToRight
} AniamteOverStatus;



@interface NewAniView ()

@end

@implementation NewAniView
@synthesize m_Delegate;
-(id) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        p_ShowViews = [[NSMutableArray alloc] init];
        p_ShadowImg=[UIImage imageNamed:@"shadow.png"];
        
        p_PageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 360, 320, 20)];
        [self addSubview:p_PageControl];
        
        p_ImgDicArr = [[NSMutableDictionary alloc] init];
        
        UISwipeGestureRecognizer* tSwipeLeftGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
        tSwipeLeftGes.delegate =self;
        tSwipeLeftGes.direction =UISwipeGestureRecognizerDirectionLeft;
        [self addGestureRecognizer:tSwipeLeftGes];
        [tSwipeLeftGes release];
        
        UISwipeGestureRecognizer* tSwipeRightGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
        tSwipeRightGes.delegate =self;
        tSwipeRightGes.direction =UISwipeGestureRecognizerDirectionRight;
        [self addGestureRecognizer:tSwipeRightGes];
        [tSwipeRightGes release];
        p_IsMoving= NO;
    }
    return self;
}

-(void) dealloc
{
    [p_ImgDicArr release];
    [p_PageControl release];
    [p_ShowViews release];
    [p_PageDataDicArr release];
    [super dealloc];
}

-(void) swipeGesture:(UISwipeGestureRecognizer*)f_Swipe
{
    if (f_Swipe.direction ==UISwipeGestureRecognizerDirectionLeft) {
        [self animationCheck:-320];
    }else if(f_Swipe.direction ==UISwipeGestureRecognizerDirectionRight){
        [self animationCheck:320];
    }
}

-(void) setDataForMyViews:(NSArray*) f_Datas
{
    if (p_PageDataDicArr) {
        [p_PageDataDicArr release];
        p_PageDataDicArr =nil;
    }
    p_PageDataDicArr = f_Datas;
    [p_PageDataDicArr retain];
    p_ShowIndex = 0;
    p_BackShowLeftIndex = [p_PageDataDicArr count]/2;//后面显示下标为前台的一半
    p_BackShowRightIndex = p_BackShowLeftIndex-1;
    
    p_PageControl.numberOfPages = [p_PageDataDicArr count];
    p_PageControl.currentPage =0;
    int tIndex = 0;

    NSLog(@"%d",[f_Datas count]);
    for (NSDictionary* tDic in f_Datas) {
        UIView* tShow = [[UIView alloc] initWithFrame:CGRectMake(50+320, 40, ShowViewWidth, ShowViewHeight)];
        [tShow setBackgroundColor:[UIColor whiteColor]];
        tShow.layer.shadowColor = [UIColor grayColor].CGColor;
        
        UIImageView* tImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ShowViewWidth, ShowViewHeight-110)];
        tImgView.tag =ShowImgTag;
        [tShow addSubview:tImgView];
        [tImgView release];
        
        UILabel* tBigTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, ShowViewHeight-110+4, ShowViewWidth-20, 22)];
        [tBigTitle setFont:[UIFont boldSystemFontOfSize:20]];
        [tBigTitle setBackgroundColor:[UIColor clearColor]];
        tBigTitle.text = [tDic objectForKey:@"name"];
        [tShow addSubview:tBigTitle];
        [tBigTitle release];
        
        UILabel* tBigEn = [[UILabel alloc] initWithFrame:CGRectMake(10, ShowViewHeight-110+24, ShowViewWidth-20, 22)];
        [tBigEn setFont:[UIFont boldSystemFontOfSize:20]];
        [tBigEn setBackgroundColor:[UIColor clearColor]];
        tBigEn.text = [tDic objectForKey:@"subname"];
        [tShow addSubview:tBigEn];
        [tBigEn release];
        
        UILabel* tSmallEn0 = [[UILabel alloc] initWithFrame:CGRectMake(10, ShowViewHeight-110+44, ShowViewWidth-20, 14)];
        [tSmallEn0 setFont:[UIFont systemFontOfSize:12]];
        [tSmallEn0 setBackgroundColor:[UIColor clearColor]];
        [tSmallEn0 setTextColor:[UIColor grayColor]];
        tSmallEn0.text = [tDic objectForKey:@"summeryOne"];
        [tShow addSubview:tSmallEn0];
        [tSmallEn0 release];
        
        UILabel* tSmallEn1 = [[UILabel alloc] initWithFrame:CGRectMake(10, ShowViewHeight-110+58, ShowViewWidth-20, 14)];
        [tSmallEn1 setFont:[UIFont systemFontOfSize:12]];
        [tSmallEn1 setBackgroundColor:[UIColor clearColor]];
        [tSmallEn1 setTextColor:[UIColor grayColor]];
        tSmallEn1.text = [tDic objectForKey:@"summeryTow"];
        [tShow addSubview:tSmallEn1];
        [tSmallEn1 release];
        
        
        UIButton* tInBtt = [UIButton buttonWithType:UIButtonTypeCustom];
        tInBtt.frame =CGRectMake(15, ShowViewHeight-36, ShowViewWidth-26, 26);
        tInBtt.tag =tIndex;
        tShow.userInteractionEnabled =YES;
        CAGradientLayer* tGradlayer =[CAGradientLayer layer];
        tGradlayer.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:79/255.f green:79/255.f blue:79/255.f alpha:1.0f].CGColor,
                             (id)[UIColor colorWithRed:6/255.f green:6/255.f blue:6/255.f alpha:1.0f].CGColor ,nil];
        tGradlayer.frame =CGRectMake(0,0, ShowViewWidth-26, 26);
        [tInBtt.layer addSublayer:tGradlayer];
        [tInBtt addTarget:self action:@selector(pressedInButton:) forControlEvents:UIControlEventTouchUpInside];
        
        
        UILabel* tInText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ShowViewWidth-26, 26)];
        [tInText setFont:[UIFont systemFontOfSize:18]];
        [tInText setTextAlignment:UITextAlignmentCenter];
        [tInText setBackgroundColor:[UIColor clearColor]];
        [tInText setTextColor:[UIColor whiteColor]];
        [tInText setText:@"进入"];
        [tInBtt addSubview:tInText];
        [tInText release];
        
        [tShow addSubview:tInBtt];
        
//        CALayer* tShadow =[CALayer layer];
//        tShadow.frame =CGRectMake(-5, 0, 5, ShowViewHeight);
//        tShadow.contents = (id)p_ShadowImg.CGImage;
//        [tShow.layer addSublayer:tShadow];
//        
//        CALayer* tRShadow =[CALayer layer];
//        tRShadow.frame =CGRectMake(ShowViewWidth+5, 0, 5, ShowViewHeight);
//        tRShadow.contents = (id)p_ShadowImg.CGImage;
//        tRShadow.transform =CATransform3DMakeRotation(M_PI_2, 0, 1, 0);
//        [tShow.layer addSublayer:tRShadow];
        
        [p_ShowViews addObject:tShow];
        tShow.tag =tIndex;
        tIndex++;
        [tShow release];
    }
    int tBLeft = p_BackShowLeftIndex+1;
    int tBRight = p_BackShowRightIndex-1;
    if (tBRight < 0) {
        tBRight = [p_ShowViews count]-1;
    }
    if (tBLeft >= [p_ShowViews count]) {
        tBLeft =0;
    }
    
    UIView * tBackSeeLView = [p_ShowViews objectAtIndex:p_BackShowLeftIndex];
    tBackSeeLView.layer.transform = CATransform3DMakeScale(ViewScaleValue,ViewScaleValue,ViewScaleValue);
    
    tBackSeeLView.frame =CGRectMake(50-80, 40, ShowViewWidth, ShowViewHeight);
    UIView * tBackSeeRView = [p_ShowViews objectAtIndex:p_BackShowRightIndex];
    tBackSeeRView.layer.transform = CATransform3DMakeScale(ViewScaleValue,ViewScaleValue,ViewScaleValue);
    tBackSeeRView.frame =CGRectMake(50+80, 40, ShowViewWidth, ShowViewHeight);
    UIView * tBLView = [p_ShowViews objectAtIndex:tBLeft];
    tBLView.layer.transform = CATransform3DMakeScale(ViewScaleValue,ViewScaleValue,
                                                     ViewScaleValue);
    UIView * tBRView = [p_ShowViews objectAtIndex:tBRight];
    tBRView.layer.transform = CATransform3DMakeScale(ViewScaleValue,ViewScaleValue,
                                                     ViewScaleValue);
    
    [self addSubview:tBackSeeLView];
    [self addSubview:tBackSeeRView];
    [self addSubview:tBLView];
    [self addSubview:tBRView];
    
    
    
    int tSLeft = p_ShowIndex-1;
    int tSRight = p_ShowIndex+1;
    if (tSLeft < 0) {
        tSLeft = [p_ShowViews count]-1;
    }
    if (tSRight >= [p_ShowViews count]) {
        tSRight =0;
    }
    
    UIView * tShowView = [p_ShowViews objectAtIndex:p_ShowIndex];
    tShowView.frame =CGRectMake(50, 40, ShowViewWidth, ShowViewHeight);
    UIView * tShowLView = [p_ShowViews objectAtIndex:tSLeft];
    tShowLView.frame =CGRectMake(50-320, 40, ShowViewWidth, ShowViewHeight);
    UIView * tShowRView = [p_ShowViews objectAtIndex:tSRight];
    tShowRView.frame =CGRectMake(50+320, 40, ShowViewWidth, ShowViewHeight);

    
    [self addSubview:tShowView];
    [self addSubview:tShowLView];
    [self addSubview:tShowRView];
    
    p_AnimateOverState = AniamteOverStatus_None;
}

- (void)setImage:(UIImage *)image forIndex:(int)index 
{
    [p_ImgDicArr setValue:image forKey:[NSString stringWithFormat:@"%d",index]];
    UIView* tShowView =[p_ShowViews objectAtIndex:index];
    UIImageView* tImgView = (UIImageView*)[tShowView viewWithTag:ShowImgTag];
    [tImgView setImage:image];
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [event.allTouches anyObject];
	p_TouchBegan = [touch locationInView:self];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [event.allTouches anyObject];
	CGPoint tMoveTouch = [touch locationInView:self];
    float tMoveDis = tMoveTouch.x -p_TouchBegan.x;
    float tBackMoveDis = tMoveDis*ViewScaleValue;
    NSLog(@"%f",tMoveDis);
    p_IsMoving = NO;
    if (abs(tMoveTouch.x - p_TouchBegan.x)> 30) {
        p_IsMoving =YES;
    }
    
    int tBLeft = p_BackShowLeftIndex+1;
    int tBRight = p_BackShowRightIndex-1;
    if (tBRight < 0) {
        tBRight = [p_ShowViews count]-1;
    }
    if (tBLeft >= [p_ShowViews count]) {
        tBLeft =0;
    }
    
    UIView * tBackSeeLView = [p_ShowViews objectAtIndex:p_BackShowLeftIndex];
    UIView * tBackSeeRView = [p_ShowViews objectAtIndex:p_BackShowRightIndex];
    UIView * tBLView = [p_ShowViews objectAtIndex:tBLeft];
    UIView * tBRView = [p_ShowViews objectAtIndex:tBRight];
    
    int tSLeft = p_ShowIndex-1;
    int tSRight = p_ShowIndex+1;
    if (tSLeft < 0) {
        tSLeft = [p_ShowViews count]-1;
    }
    if (tSRight >= [p_ShowViews count]) {
        tSRight =0;
    }
    UIView * tShowView = [p_ShowViews objectAtIndex:p_ShowIndex];
    UIView * tShowLView = [p_ShowViews objectAtIndex:tSLeft];
    UIView * tShowRView = [p_ShowViews objectAtIndex:tSRight];
    
    
    

    tShowView.frame = CGRectMake(50+tMoveDis, 40, ShowViewWidth, ShowViewHeight);
    tShowLView.frame = CGRectMake(50+tMoveDis-320, 40, ShowViewWidth, ShowViewHeight);
    tShowRView.frame = CGRectMake(50+tMoveDis+320, 40, ShowViewWidth, ShowViewHeight);
    
    tBackSeeLView.frame = CGRectMake(50-80-tBackMoveDis, 40, ShowViewWidth, ShowViewHeight);
    tBackSeeRView.frame = CGRectMake(50+80-tBackMoveDis, 40, ShowViewWidth, ShowViewHeight);
    tBLView.frame = CGRectMake(50-240-tBackMoveDis, 40, ShowViewWidth, ShowViewHeight);
    tBRView.frame = CGRectMake(50+240-tBackMoveDis, 40, ShowViewWidth, ShowViewHeight);

}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [event.allTouches anyObject];
	CGPoint tTouchEnd = [touch locationInView:self];
    float tMoveDis = tTouchEnd.x -p_TouchBegan.x;
    [self animationCheck:tMoveDis];
}

-(void) animationCheck:(NSInteger) f_Dis;
{
    int tBLeft = p_BackShowLeftIndex+1;
    int tBRight = p_BackShowRightIndex-1;
    if (tBRight < 0) {
        tBRight = [p_ShowViews count]-1;
    }
    if (tBLeft >= [p_ShowViews count]) {
        tBLeft =0;
    }
    
    UIView * tBackSeeLView = [p_ShowViews objectAtIndex:p_BackShowLeftIndex];
    UIView * tBackSeeRView = [p_ShowViews objectAtIndex:p_BackShowRightIndex];
    UIView * tBLView = [p_ShowViews objectAtIndex:tBLeft];
    UIView * tBRView = [p_ShowViews objectAtIndex:tBRight];
    
    int tSLeft = p_ShowIndex-1;
    int tSRight = p_ShowIndex+1;
    if (tSLeft < 0) {
        tSLeft = [p_ShowViews count]-1;
    }
    if (tSRight >= [p_ShowViews count]) {
        tSRight =0;
    }
    UIView * tShowView = [p_ShowViews objectAtIndex:p_ShowIndex];
    UIView * tShowLView = [p_ShowViews objectAtIndex:tSLeft];
    UIView * tShowRView = [p_ShowViews objectAtIndex:tSRight];
    p_AnimateOverState = AniamteOverStatus_None;
    float tBackMoveDis = 0;
    if (f_Dis >140) {
        f_Dis = 320;
        tBackMoveDis = 320*ViewScaleValue;
        p_AnimateOverState = AniamteOverStatus_ToRight;
    }else if(f_Dis <-140){
        f_Dis = -320;
        tBackMoveDis = -320*ViewScaleValue;
        p_AnimateOverState = AniamteOverStatus_ToLeft;
    }else {
        f_Dis = 0;
        tBackMoveDis = 0;
    }
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(checkViews)];
    tShowView.frame = CGRectMake(50+f_Dis, 40, ShowViewWidth, ShowViewHeight);
    tShowLView.frame = CGRectMake(50+f_Dis-320, 40, ShowViewWidth, ShowViewHeight);
    tShowRView.frame = CGRectMake(50+f_Dis+320, 40, ShowViewWidth, ShowViewHeight);
    
    tBackSeeLView.frame = CGRectMake(50-80-tBackMoveDis, 40, ShowViewWidth, ShowViewHeight);
    tBackSeeRView.frame = CGRectMake(50+80-tBackMoveDis, 40, ShowViewWidth, ShowViewHeight);
    tBLView.frame = CGRectMake(50-240-tBackMoveDis, 40, ShowViewWidth, ShowViewHeight);
    tBRView.frame = CGRectMake(50+240-tBackMoveDis, 40, ShowViewWidth, ShowViewHeight);
    [UIView commitAnimations];
}

-(void)checkViews
{
    if (p_AnimateOverState != AniamteOverStatus_None) {
        int tBLeft = p_BackShowLeftIndex+1;
        int tBRight = p_BackShowRightIndex-1;
        if (tBRight < 0) {
            tBRight = [p_ShowViews count]-1;
        }
        if (tBLeft >= [p_ShowViews count]) {
            tBLeft =0;
        }
        
        UIView * tBackSeeLView = [p_ShowViews objectAtIndex:p_BackShowLeftIndex];
        UIView * tBackSeeRView = [p_ShowViews objectAtIndex:p_BackShowRightIndex];
        UIView * tBLView = [p_ShowViews objectAtIndex:tBLeft];
        UIView * tBRView = [p_ShowViews objectAtIndex:tBRight];
        [tBackSeeLView removeFromSuperview];
        [tBackSeeRView removeFromSuperview];
        [tBLView removeFromSuperview];
        [tBRView removeFromSuperview];
        int tSLeft = p_ShowIndex-1;
        int tSRight = p_ShowIndex+1;
        if (tSLeft < 0) {
            tSLeft = [p_ShowViews count]-1;
        }
        if (tSRight >= [p_ShowViews count]) {
            tSRight =0;
        }
        UIView * tShowView = [p_ShowViews objectAtIndex:p_ShowIndex];
        UIView * tShowLView = [p_ShowViews objectAtIndex:tSLeft];
        UIView * tShowRView = [p_ShowViews objectAtIndex:tSRight];
        [tShowView removeFromSuperview];
        [tShowLView removeFromSuperview];
        [tShowRView removeFromSuperview];
    }
    switch (p_AnimateOverState) {
        case AniamteOverStatus_None:
            
            break;
        case AniamteOverStatus_ToRight:
        {
            p_ShowIndex--;
            if (p_ShowIndex < 0) {
                p_ShowIndex = [p_ShowViews count]-1;
            }
            p_BackShowLeftIndex--;
            if (p_BackShowLeftIndex < 0) {
                p_BackShowLeftIndex = [p_ShowViews count]-1;
            }
            p_BackShowRightIndex--;
            if (p_BackShowRightIndex < 0) {
                p_BackShowRightIndex = [p_ShowViews count]-1;
            }
        }
            break;
        case AniamteOverStatus_ToLeft:
        {
            p_ShowIndex++;
            if (p_ShowIndex >= [p_ShowViews count]) {
                p_ShowIndex = 0;
            }

            
            p_BackShowLeftIndex++;
            if (p_BackShowLeftIndex >=[p_ShowViews count]) {
                p_BackShowLeftIndex = 0;
            }
            p_BackShowRightIndex++;
            if (p_BackShowRightIndex >=[p_ShowViews count]) {
                p_BackShowRightIndex = 0;
            }
        }
            break;
    }
    p_PageControl.currentPage =p_ShowIndex;
    if (p_AnimateOverState != AniamteOverStatus_None) {
        for (UIView* tView in p_ShowViews) {
            tView.layer.transform = CATransform3DIdentity;
        }
        
        int tBLeft = p_BackShowLeftIndex+1;
        int tBRight = p_BackShowRightIndex-1;
        if (tBRight < 0) {
            tBRight = [p_ShowViews count]-1;
        }
        if (tBLeft >= [p_ShowViews count]) {
            tBLeft =0;
        }
        UIView * tBackSeeLView = [p_ShowViews objectAtIndex:p_BackShowLeftIndex];
        tBackSeeLView.layer.transform = CATransform3DMakeScale(ViewScaleValue,ViewScaleValue,ViewScaleValue);
        
        tBackSeeLView.frame =CGRectMake(50-80, 40, ShowViewWidth, ShowViewHeight);
        UIView * tBackSeeRView = [p_ShowViews objectAtIndex:p_BackShowRightIndex];
        tBackSeeRView.layer.transform = CATransform3DMakeScale(ViewScaleValue,ViewScaleValue,ViewScaleValue);
        tBackSeeRView.frame =CGRectMake(50+80, 40, ShowViewWidth, ShowViewHeight);
        UIView * tBLView = [p_ShowViews objectAtIndex:tBLeft];
        tBLView.layer.transform = CATransform3DMakeScale(ViewScaleValue,ViewScaleValue,
                                                         ViewScaleValue);
        UIView * tBRView = [p_ShowViews objectAtIndex:tBRight];
        tBRView.layer.transform = CATransform3DMakeScale(ViewScaleValue,ViewScaleValue,
                                                         ViewScaleValue);
        
        [self addSubview:tBackSeeLView];
        [self addSubview:tBackSeeRView];
        [self addSubview:tBLView];
        [self addSubview:tBRView];
        

        int tSLeft = p_ShowIndex-1;
        int tSRight = p_ShowIndex+1;
        if (tSLeft < 0) {
            tSLeft = [p_ShowViews count]-1;
        }
        if (tSRight >= [p_ShowViews count]) {
            tSRight =0;
        }
        UIView * tShowView = [p_ShowViews objectAtIndex:p_ShowIndex];
        tShowView.frame =CGRectMake(50, 40, ShowViewWidth, ShowViewHeight);
        UIView * tShowLView = [p_ShowViews objectAtIndex:tSLeft];
        tShowLView.frame =CGRectMake(50-320, 40, ShowViewWidth, ShowViewHeight);
        UIView * tShowRView = [p_ShowViews objectAtIndex:tSRight];
        tShowRView.frame =CGRectMake(50+320, 40, ShowViewWidth, ShowViewHeight);
        
        [self addSubview:tShowView];
        [self addSubview:tShowLView];
        [self addSubview:tShowRView];

    }
}



- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]]) {
        UISwipeGestureRecognizer* tSwipe =(UISwipeGestureRecognizer*)gestureRecognizer;
        if ((tSwipe.direction == UISwipeGestureRecognizerDirectionLeft||
             tSwipe.direction == UISwipeGestureRecognizerDirectionRight) && !p_IsMoving) {
            return YES;
        }
    }
    return NO;
}


-(void)pressedInButton:(UIButton*) sender
{
    [m_Delegate performSelector:@selector(clickOnPageForIndex:) withObject:[NSNumber numberWithInt:sender.tag]];
}

@end
